package com.community.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.community.entity.Postings;
import com.community.service.IPostingsService;

@Controller
@RequestMapping("/forum")
public class ForumController {
	
	@Autowired
	private IPostingsService ipostingService;
	
	@RequestMapping()
	public String forum(ModelMap modelMap){
		List<Postings> pList = ipostingService.getPostingsList();
		modelMap.addAttribute("pList", pList);
		return "/forum/forum";
		
	}
	
	
	@RequestMapping("/tiezi")
	public String tiezi(){
		return "/forum/tiezi";
		
	}

	@RequestMapping("/add")
	public String addpostings(String tittle,String content,HttpSession session){
		
		if(session.getAttribute("userid") == null){
			return "/user/login";
		}else{
			Long id = Long.valueOf(session.getAttribute("userid").toString());
		Postings p = new Postings();
		p.setContent(content);
		p.setTittle(tittle);
		p.setUserId(id);
		ipostingService.addPostings(p);
		
		return "redirect:/forum";
		}
	}
	
	@RequestMapping("/mypostings")
	public String myPostings(ModelMap modelMap,HttpSession session){
		
		if(session.getAttribute("userid") == null){
			return "/user/login";
		}else{
			Long id = Long.valueOf(session.getAttribute("userid").toString());
			List<Postings> pList = ipostingService.getPostingsListByUserId(id);
			modelMap.addAttribute("pList", pList);
			return "/forum/myforum";
			
		}
	}
	
	@RequestMapping("/delete/{id}")
	public String delete(@PathVariable Long id){
		Postings p = ipostingService.getPostings(id);
		ipostingService.delete(p);
		return "redirect:/forum/mypostings";
	}
	
	@RequestMapping("/detail/{id}")
	public String detail(ModelMap modelMap,@PathVariable Long id){
		Postings p = ipostingService.getPostings(id);
		modelMap.addAttribute("p", p);
		
		return "/forum/tiezi_detail";
		
	}
	
}
